package icu.wdtt.Interceptors;

import cn.dev33.satoken.stp.StpUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import icu.wdtt.Utils.ResultUtils;
import icu.wdtt.model.User;
import icu.wdtt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Autowired
    private UserService userService;
    @Autowired
    private ResultUtils resultUtils;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //这是请求头和返回头的字符格式以及响应头的信息
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        //创建Jackson解析器对象对象
        ObjectMapper mapper = new ObjectMapper();
        //判断之前是否登陆过
        if(StpUtil.isLogin()){
            //这里查询用户是否登陆
            return true;
        }
        //这里未登陆返回信息
        mapper.writeValue(response.getWriter(),resultUtils.fail("用户未登陆",401));
       return false;
    }
}
